package com.longcai.cm.base.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.longcai.cm.base.bean.PageVo;
import com.longcai.cm.base.bean.Pageable;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Map;

public abstract class BaseService<T, ID extends Serializable> extends ServiceImpl<BaseMapper<T>, T> implements BaseServiceIface<T> {

    private Logger logger = LoggerFactory.getLogger(getClass());

    public BaseMapper<T> baseDao;


    @Override
    public BaseMapper<T> getBaseDao() {
        return baseDao;
    }

    public void setBaseDao(BaseMapper<T> baseDao) {
        this.baseDao = baseDao;
    }


    @Override
    public PageVo<T> getPageBean(Pageable pageable, Class<T> clazz) {
        return getPageBean(pageable, pageable.getPageSearch(), clazz);
    }


    @Override
    public PageVo<T> getPageBean(Pageable pageable, Map<String, Object> queryMap, Class<T> clazz) {

        logger.debug("【分页获取列表信息：】startPage信息" + pageable.getPageIndex() + "--" + pageable.getPageSize());

        QueryWrapper<T> eWrapper = new QueryWrapper<>();
        try {
            eWrapper.setEntity(clazz.newInstance());
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }

        //拼接查询条件
        if (queryMap != null) {
            for (String key : queryMap.keySet()) {
                String value = queryMap.get(key).toString();
                if (StringUtils.isNotBlank(value)) {
                    String[] conditionStrings = key.split("_");
                    String condition = conditionStrings[1];
                    //存在一种情况：查找的字段中带有下划线，所以截取第二个“_”之后的所有字符
                    String fieldString = key.substring(key.indexOf("_", key.indexOf("_") + 1) + 1);
                    // 条件信息
                    if ("LIKE".equals(condition)) {
                        eWrapper.like(fieldString, value);
                    } else if ("EQ".equals(condition)) {
                        eWrapper.eq(fieldString, value);
                    } else if ("NOTEQ".equals(condition)) {
                        eWrapper.ne(fieldString, value);
                    } else if ("IN".equals(condition)) {
                        eWrapper.in(fieldString, value);
                    } else if ("GE".equals(condition)) { //大于等于
                            eWrapper.ge(fieldString, value);
                    } else if ("LE".equals(condition)) { //小于等于
                            eWrapper.le(fieldString, value);
                    }
                }
            }

        }

        Map<String, Boolean> orderParam = pageable.getPageOrderParam();
        if (orderParam != null) {
            for (String key : orderParam.keySet()) {
                Boolean ascFlag = orderParam.get(key);
                eWrapper.orderBy(true, ascFlag, key);
            }
        }

        //pageHelper插件，分页封装
        PageHelper.startPage(pageable.getPageIndex(), pageable.getPageSize(), true);//
        PageInfo<T> pageInfo = new PageInfo<T>(super.getBaseMapper().selectList(eWrapper));
        return new PageVo<>(pageInfo);

    }


}
